//
//  LSZVideoClassShowViewController.m
//  PartyClassCloud
//
//  Created by 梁素珍 on 2018/8/25.
//  Copyright © 2018年 九酷. All rights reserved.
//

#import "LSZVideoClassShowViewController.h"

@interface LSZVideoClassShowViewController ()<UICollectionViewDelegate,UICollectionViewDataSource>
@property(nonatomic,strong) UICollectionView * collectionViewStudyPlan;
@property(nonatomic,strong) NSMutableArray * arrayData;
@property(nonatomic,assign) NSInteger page;
@end

@implementation LSZVideoClassShowViewController

#pragma mark - 获取数据
-(void)getDataStudyPlan:(NSInteger)page
{
    [LSZRequestDicData secondaryInterfaceForData:self.id_id page:page :^(BOOL isOK, NSString * string, LSZSecondaryInterfaceManager * mg) {
        if (isOK == YES) {
            if(page == 0){
                [self.arrayData removeAllObjects];
                [self.arrayData addObjectsFromArray:mg.arrayData];
            }else{
                [self.arrayData addObjectsFromArray:mg.arrayData];
            }
            [self.collectionViewStudyPlan reloadData];
        }else{
            if ([string isEqualToString:@"重复登录"]) {
                [LSZPublicClassDataToll RepeatLoginInterfaceJump:self.view fromVC:@"LSZVideoClassShowViewController"];
            }else{
                [LSZPublicClassDataToll initWithAlertKuangView:self.view string:string];
            }
        }
    }];
}
-(void)goToLogin
{
    [LSZPublicClassDataToll goToLogin];
}
-(void)addNotificationCenter
{
    NSString * strDelete = [NSString stringWithFormat:@"%@Login",@"LSZVideoClassShowViewController"];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(goToLogin) name:strDelete object:nil];
}
- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"视频专题";
    [self addNotificationCenter];
    self.page = 0;
    self.arrayData = [[NSMutableArray alloc]init];
    UICollectionViewFlowLayout * flowLayout = [[UICollectionViewFlowLayout alloc]init];
    flowLayout.minimumInteritemSpacing = 0.5;
    flowLayout.minimumLineSpacing = 0.5;
    flowLayout.itemSize = CGSizeMake(ScreenWidth-20,200);
    flowLayout.sectionInset = UIEdgeInsetsMake(10,10,10, 10);//分别为上、左、下、右
    self.collectionViewStudyPlan = [[UICollectionView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-44-NAVIGATION_BAR_HEIGHT) collectionViewLayout:flowLayout];
    self.collectionViewStudyPlan.dataSource = self;
    self.collectionViewStudyPlan.delegate = self;
    self.collectionViewStudyPlan.backgroundColor = RGBColor(236, 236, 236);
    //添加以下代码关闭估算行高
    [self.view addSubview:self.collectionViewStudyPlan];
    [self.collectionViewStudyPlan registerClass:[LSZStudyPlanCollectionViewCell class] forCellWithReuseIdentifier:@"LSZStudyPlanCollectionViewCell"];
    [self getDataStudyPlan:self.page];
    /*
    __weak typeof(self) weakSelf = self;
    [weakSelf.collectionViewStudyPlan addRefreshHeaderWithHandle:^{
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            // 刷新表格  和结束刷新
            weakSelf.page = 0;
            [weakSelf getDataStudyPlan:weakSelf.page];
            [weakSelf.collectionViewStudyPlan.mj_header endRefreshing];
        });
    }];
    weakSelf.collectionViewStudyPlan.mj_footer = [MJRefreshAutoNormalFooter footerWithRefreshingBlock:^{
        //延时
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.0 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            self.page++;
            [self getDataStudyPlan:self.page];
            [self.collectionViewStudyPlan.mj_footer endRefreshing];
        });
    }];
    */
}
#pragma mark - 设置有几个区
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView {
    return self.arrayData.count;
}
#pragma mark - 设置每个区有几个
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    // 定义的轮播器视图
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    LSZStudyPlanCollectionViewCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"LSZStudyPlanCollectionViewCell" forIndexPath:indexPath];
    if (self.arrayData.count > indexPath.row) {
        LSZSecondaryInterfaceMg * mg = nil;
        mg = self.arrayData[indexPath.section];
        cell.secondaryInterfaceMg = mg;
    }
    return cell;
}
#pragma mark - 点击进入详情页
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if(self.arrayData.count > indexPath.row){
        LSZVideoPlaybackViewController * vc = [[LSZVideoPlaybackViewController alloc]init];
        LSZSecondaryInterfaceMg * mg = nil;
        mg = self.arrayData[indexPath.section];
        vc.id_id = mg.id_id;
        vc.imageViewOver = mg.coverUrl;
        vc.introductionStr = mg.summary;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

@end
